package com.lemon.business.dao;

import com.lemon.entity.CpuDataVO;
import com.lemon.entity.DiskDataVO;
import com.lemon.entity.MachineVO;
import com.lemon.entity.MemoryDataVO;
import com.lemon.utils.DBUtil;
import com.lemon.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.List;

/**
 * @Author liuli
 * @Date 2020/10/14 22:05
 */
public class InsertDataDao {
    private static final Logger LOG = LoggerFactory.getLogger(InsertDataDao.class);

    private static final int TIME_OUT = 30;

    private static final String INSERT_CPU_SQL = "INSERT INTO cpu_data_source(upload_token, cpu_model, data_time, cpu_usage_rate, cpu_logic_core_number, aggre_field_10mini) VALUES(?, ?, ?, ?, ?, ?)";

    private static final String INSERT_DISK_SQL = "INSERT INTO disk_data_source(upload_token, dev_name, data_time, disk_use, disk_total, aggre_field_10mini) VALUES(?, ?, ?, ?, ?, ?)";

    private static final String INSERT_MACHINE_SQL = "INSERT INTO machine(upload_token, account, memory_total, os_name, pc_name) VALUESVALUES(?, ?, ?, ?, ?)";

    private static final String UPDATE_MACHINE_SQL = "UPDATE machine set account=?, memory_total=?, os_name=?, pc_name=?  where upload_token=?";

    private static final String INSERT_MEMORY_SQL = "INSERT INTO memory_data_source(upload_token, data_time, memory_use, memory_total, aggre_field_10mini) VALUES(?, ?, ?, ?, ?)";

    public static boolean insertCpu(List<CpuDataVO> cpuDataVOS) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            statement = connection.prepareStatement(INSERT_CPU_SQL);
            for (CpuDataVO cpuData : cpuDataVOS) {
                statement.setString(1, cpuData.getToken());
                statement.setString(2, cpuData.getCpuModel());
                statement.setTimestamp(3, new Timestamp(cpuData.getDataTime()));
                statement.setLong(4, cpuData.getAvgUsageRate());
                statement.setLong(5, cpuData.getLogicCoreNumber());
                statement.setString(6, DateUtil.get10MiniGranByTime(cpuData.getDataTime()));
                statement.addBatch();
            }
            statement.setQueryTimeout(TIME_OUT);
            statement.executeUpdate();;
            connection.commit();
            LOG.info("insert cpu data count : {}", cpuDataVOS.size());
            return true;
        } catch (Exception e) {
            LOG.error("insertCpu error", e);
            return false;
        } finally {
            DBUtil.close(resultSet, statement, connection);
        }
    }

    public static boolean insertDisk(List<DiskDataVO> diskDataVOS) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            statement = connection.prepareStatement(INSERT_DISK_SQL);
            for (DiskDataVO diskData : diskDataVOS) {
                statement.setString(1, diskData.getToken());
                statement.setString(2, diskData.getDevName());
                statement.setTimestamp(3, new Timestamp(diskData.getDataTime()));
                statement.setLong(4, diskData.getUse());
                statement.setLong(5, diskData.getTotal());
                statement.setString(6, DateUtil.get10MiniGranByTime(diskData.getDataTime()));
                statement.addBatch();
            }
            statement.setQueryTimeout(TIME_OUT);
            statement.executeBatch();;
            connection.commit();
            LOG.info("insert disk data count : {}", diskDataVOS.size());
            return true;
        } catch (Exception e) {
            LOG.error("insertDisk error", e);
            return false;
        } finally {
            DBUtil.close(resultSet, statement, connection);
        }
    }

    public static boolean updateMachine(List<MachineVO> machineVOS) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            statement = connection.prepareStatement(UPDATE_MACHINE_SQL);
            for (MachineVO memoryData : machineVOS) {
                statement.setString(1, memoryData.getAccount());
                statement.setString(2, memoryData.getMemoryTotal());
                statement.setString(3, memoryData.getOsName());
                statement.setString(4, memoryData.getPcName());
                statement.setString(5, memoryData.getToken());
                statement.addBatch();
            }
            statement.setQueryTimeout(TIME_OUT);
            statement.executeBatch();;
            connection.commit();
            LOG.info("insert memory data count : {}", machineVOS.size());
            return true;
        } catch (Exception e) {
            LOG.error("insertMemory error", e);
            return false;
        } finally {
            DBUtil.close(resultSet, statement, connection);
        }
    }

    public static boolean insertMemory(List<MemoryDataVO> memoryDataVOS) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            statement = connection.prepareStatement(INSERT_MEMORY_SQL);
            for (MemoryDataVO memoryData : memoryDataVOS) {
                statement.setString(1, memoryData.getToken());
                statement.setTimestamp(2, new Timestamp(memoryData.getDataTime()));
                statement.setLong(3, memoryData.getUse());
                statement.setLong(4, memoryData.getTotal());
                statement.setString(5, DateUtil.get10MiniGranByTime(memoryData.getDataTime()));
                statement.addBatch();
            }
            statement.setQueryTimeout(TIME_OUT);
            statement.executeBatch();;
            connection.commit();
            LOG.info("insert memory data count : {}", memoryDataVOS.size());
            return true;
        } catch (Exception e) {
            LOG.error("insertMemory error", e);
            return false;
        } finally {
            DBUtil.close(resultSet, statement, connection);
        }
    }

}
